//
//  Hear_NewestViewCell.m
//  Hear
//
//  Created by mac on 2018/5/4.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "Hear_NewestViewCell.h"

@interface Hear_NewestViewCell()

/** 标题 */
@property (nonatomic, strong) UILabel *titleLB;
/** 话题题主姓名 */
@property (nonatomic, strong) UILabel *owner;
/** 转动头像 */
@property (nonatomic, strong) UIImageView *rotatingImg;
/** 性别图片 */
@property (nonatomic, strong) UIImageView *seximg;
/** 题主身份 */
@property (nonatomic, strong) UILabel *year;

/** 播放器 */
@property (nonatomic, strong) AVPlayer *player;

@property (nonatomic, strong) AVPlayerItem *item;

@end

@implementation Hear_NewestViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    UIView *topicView = [UIView new];
    topicView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.3];
    topicView.layer.cornerRadius = 10 * scaleWidth;
    topicView.layer.masksToBounds = YES;
    [self.contentView addSubview:topicView];
    
    UIView *playerView = [UIView new];
    playerView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    playerView.layer.cornerRadius = 10 * scaleWidth;
    playerView.layer.masksToBounds = YES;
    [self.contentView addSubview:playerView];
    [self.contentView bringSubviewToFront:playerView];
    
    [topicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView.mas_leading).mas_offset(12 * scaleWidth);
        make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-12 * scaleWidth);
        make.top.mas_equalTo(self.contentView.mas_top).mas_offset(10 * scaleHeight);
        make.height.mas_equalTo(104 * scaleHeight);
    }];
    
    [playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-10 * scaleHeight);
        make.centerX.mas_equalTo(topicView);
        make.size.mas_equalTo(CGSizeMake(287 * scaleWidth, 55 * scaleHeight));
    }];
    
    UILabel *titleLB = [UILabel new];
    titleLB.font = CustomUIFont(14);
    titleLB.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    titleLB.numberOfLines = 0;
    self.titleLB = titleLB;
    [topicView addSubview:titleLB];
    
    [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(topicView.mas_leading).mas_offset(13 * scaleWidth);
        make.trailing.mas_equalTo(topicView.mas_trailing).mas_offset(-13 * scaleWidth);
        make.top.mas_equalTo(topicView.mas_top).mas_offset(15 * scaleHeight);
        make.height.mas_equalTo(38 * scaleHeight);
    }];
    
    UIImageView *headerimg = [[UIImageView alloc] initWithImage:GetImgWithStr(@"")];
    headerimg.layer.cornerRadius = 20 * scaleWidth;
    headerimg.layer.masksToBounds = YES;
    [playerView addSubview:headerimg];
    
    [headerimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(playerView.mas_leading).mas_offset(10 * scaleWidth);
        make.centerY.mas_equalTo(playerView);
        make.size.mas_equalTo(CGSizeMake(40 * scaleWidth, 40 * scaleHeight));
    }];
    
    // 题主名称
    UILabel *owner = [UILabel new];
    owner.textColor = [UIColor colorWithHexString:@"#6E6E6E"];
    owner.font = [UIFont systemFontOfSize:14];
    owner.textAlignment = NSTextAlignmentLeft;
    owner.text = @"昵称显示";
    self.owner = owner;
    [playerView addSubview:owner];
    
    [owner mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(headerimg.mas_trailing).mas_offset(17.5 * scaleWidth);
        make.top.mas_equalTo(headerimg.mas_top);
    }];
    
    // 题主性别
    UIImageView *seximg = [[UIImageView alloc] initWithImage:GetImgWithStr(@"")];
    self.seximg = seximg;
    [playerView addSubview:seximg];
    
    [seximg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(owner.mas_leading);
        make.top.mas_equalTo(owner.mas_bottom).mas_offset(10 * scaleHeight);
        make.size.mas_equalTo(CGSizeMake(14 * scaleWidth, 14 * scaleHeight));
    }];
    
    // 题主年龄
    UILabel *year = [UILabel new];
    year.textColor = [UIColor colorWithHexString:@"#6E6E6E"];
    year.font = [UIFont systemFontOfSize:14];
    year.textAlignment = NSTextAlignmentLeft;
    year.text = @"90后";
    self.year = year;
    [playerView addSubview:year];
    
    [year mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(seximg.mas_trailing).mas_offset(8.5 * scaleWidth);
        make.centerY.mas_equalTo(seximg);
    }];
    
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [playBtn setImage:GetImgWithStr(@"Newest_暂停") forState:UIControlStateNormal];
    [playBtn addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [playerView addSubview:playBtn];
    
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(playerView.mas_trailing).mas_offset(17 * scaleWidth);
        make.centerY.mas_equalTo(playerView);
        make.size.mas_equalTo(CGSizeMake(33 * scaleWidth, 33 * scaleHeight));
    }];
    
    self.item = [[AVPlayerItem alloc] initWithURL:GetUrlWithStr(@"")];
    
    self.player = [[AVPlayer alloc] initWithPlayerItem:self.item];
}

#pragma mark 播放
- (void)playBtnAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        [sender setImage:GetImgWithStr(@"Newest_播放") forState:UIControlStateNormal];
    } else {
        [sender setImage:GetImgWithStr(@"Newest_暂停") forState:UIControlStateNormal];
    }
    
}

@end
